print("===============欢迎使用Best计算器==============")
print("-------作者:吴易阳-------")
print("-------功能强大 :一应俱全-------")
import math

# --------------------- 安全校验函数 ---------------------
def validate_positive(num, name="参数"):
    """校验数值是否为正数"""
    if num <= 0:
        print(f"错误：{name}必须大于0！")
        return False
    return True

def validate_log_base(a):
    """校验对数底数"""
    if a <= 0:
        print("错误：对数底数必须大于0！")
        return False
    if a == 1:
        print("错误：对数底数不能为1！")
        return False
    return True

# --------------------- 数学函数 ---------------------
# 三角函数
def Pysin(a):
    return math.sin(a)

def Pycos(a):
    return math.cos(a)

def Pytan(a):
    try:
        return math.tan(a)
    except:
        print("错误：正切函数输入值不合法！")
        return None

# 对数函数
def PyUg(a):  # 自然对数
    try:
        if not validate_positive(a, "自然对数参数"):
            return None
        return math.log(a)
    except Exception as e:
        print("自然对数计算错误:", e)
        return None

def Pylog(a, b):  # 以a为底的对数
    if not validate_log_base(a):
        return None
    if not validate_positive(b, "对数真数"):
        return None
    try:
        return math.log(b) / math.log(a)
    except Exception as e:
        print("对数计算错误:", e)
        return None

# 基础运算
def sum(a, b):
    return a + b

def sub(a, b):
    return a - b

def mul(a, b):
    return a * b

def div(a, b):
    if b == 0:
        print("错误：除数不能为0！")
        return None
    return a / b

# 复数运算
def Pycomplex(a, b):
    complexOper = input("请输入复数的运算(+,-,*,/): ")
    if complexOper == "+":
        result = a + b
    elif complexOper == "-":
        result = a - b
    elif complexOper == "*":
        result = a * b
    elif complexOper == "/":
        if b == 0:
            print("错误：复数除法中除数不能为0！")
            return None
        result = a / b
    else:
        print("无效运算符！")
        return None
    print(f"结果：{result}")
    return result

# --------------------- 主逻辑 ---------------------
flag = True
while flag:
    try:
        a = eval(input("请输入a: "))
        b = eval(input("请输入b: "))
    except:
        print("输入格式错误，请重新输入！")
        continue

    operator = input("请输入运算符(+,-,*,/,com,sin,cos,tan,log): ")

    if operator == "+":
        print(f"{a} + {b} = {sum(a, b)}")
    elif operator == "-":
        print(f"{a} - {b} = {sub(a, b)}")
    elif operator == "*":
        print(f"{a} * {b} = {mul(a, b)}")
    elif operator == "/":
        result = div(a, b)
        if result is not None:
            print(f"{a} / {b} = {result}")
    elif operator == "com":
        Pycomplex(a, b)
    elif operator == "sin":
        print(f"sin({a}) = {Pysin(a)}")
        print(f"sin({b}) = {Pysin(b)}")
    elif operator == "cos":
        print(f"cos({a}) = {Pycos(a)}")
        print(f"cos({b}) = {Pycos(b)}")
    elif operator == "tan":
        res_a = Pytan(a)
        res_b = Pytan(b)
        if res_a is not None: print(f"tan({a}) = {res_a}")
        if res_b is not None: print(f"tan({b}) = {res_b}")
    elif operator == "log":
        ln_a = PyUg(a)
        ln_b = PyUg(b)
        if ln_a is not None: print(f"ln({a}) = {ln_a}")
        if ln_b is not None: print(f"ln({b}) = {ln_b}")
        log_ab = Pylog(a, b)
        if log_ab is not None:
            print(f"以{a}为底{b}的对数 = {log_ab:.4f}")
    else:
        print("无效的运算符！")

    # 循环控制
    while True:
        choice = input("继续计算吗？(y/n): ").lower()
        if choice in ['y', 'n']:
            flag = (choice == 'y')
            break
        print("请输入 y 或 n！")